// Keywords: mating systems, haplodiploidy, arrhenotoky, bees, wasps, ants, Hymenoptera

initialize() {
	defineConstant("K", 2000);
	defineConstant("P_OFFSPRING_MALE", 0.8);
	initializeSLiMModelType("nonWF");
	initializeMutationRate(1e-8);
	initializeMutationType("m1", 0.0, "f", 0.0);
	m1.convertToSubstitution = T;
	m1.haploidDominanceCoeff = 1.0;
	initializeGenomicElementType("g1", m1, 1.0);
	initializeGenomicElement(g1, 0, 999999);
	initializeRecombinationRate(1e-6);
	initializeSex("A");
}
reproduction(NULL, "F") {
	gen1 = individual.genome1;
	gen2 = individual.genome2;
	breaks = sim.chromosome.drawBreakpoints(individual);
	
	// decide whether we're generating a haploid male or a diploid female
	if (rbinom(1, 1, P_OFFSPRING_MALE))
	{
		// didn't find a mate; make a haploid male from an unfertilized egg:
		//		- one genome comes from recombination of the female's genomes
		//		- the other genome is a null genome (a placeholder)
		subpop.addRecombinant(gen1, gen2, breaks, NULL, NULL, NULL, "M",
			randomizeStrands=T);
	}
	else
	{
		// found a mate; make a diploid female from a fertilized egg:
		//		- one genome comes from recombination of the female's genomes
		//		- the other genome comes from the mate (a haploid male)
		mate = subpop.sampleIndividuals(1, sex="M");
		subpop.addRecombinant(gen1, gen2, breaks, mate.genome1, NULL, NULL, "F",
			randomizeStrands=T);
	}
}
1 early() {
	// make an initial population with the right genetics
	mCount = asInteger(K * P_OFFSPRING_MALE);
	fCount = K - mCount;
	sim.addSubpop("p1", mCount, sexRatio=1.0, haploid=T);	// males
	sim.addSubpop("p2", fCount, sexRatio=0.0, haploid=F);	// females
	p1.takeMigrants(p2.individuals);
	p2.removeSubpopulation();
}
early() {
	p1.fitnessScaling = K / p1.individualCount;
}
10000 late() {
	sim.simulationFinished();
}
